package com.newpage.module_gather.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.newpage.module_gather.R;

import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Text;

import java.util.List;

/**
 * author: zhouyh
 * created on: 2021/6/22 2:48 下午
 * description:
 */
public class MasteryLevelDialogFragment extends DialogFragment implements View.OnClickListener {


    private TextView tv_examination;
    private RadioGroup rg_master_level;
    private Button bt_ok;

    private String examinationPoint;
    private int masteryLevel;


    public static MasteryLevelDialogFragment getInstance(String examinationPoint, int masteryLevel) {
        Bundle args = new Bundle();
        MasteryLevelDialogFragment fragment = new MasteryLevelDialogFragment();
        args.putString("examinationPoint", examinationPoint);
        args.putInt("masteryLevel", masteryLevel);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.60), (int) (dm.heightPixels * 0.75));
        }
    }


    @NonNull
    @NotNull
    @Override
    public Dialog onCreateDialog(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater
                                     inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup
                                     container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.gather_dialog_exam_masterylevel, container);
        tv_examination = rootView.findViewById(R.id.tv_examination);
        rg_master_level = rootView.findViewById(R.id.rg_mastery_level);
        bt_ok = rootView.findViewById(R.id.btn_dialog_ok);
        bt_ok.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View
                                      view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        examinationPoint = getArguments().getString("examinationPoint", "无");
        masteryLevel = getArguments().getInt("masteryLevel", 1);
        tv_examination.setText(examinationPoint);
        rg_master_level.check(getRadioButtonId(masteryLevel));
        rg_master_level.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                group.check(checkedId);
                if (checkedId == R.id.rb_understand) {
                    masteryLevel = 3;
                } else if (checkedId == R.id.rb_understand_half) {
                    masteryLevel = 2;
                } else if (checkedId == R.id.rb_understand_not) {
                    masteryLevel = 1;
                }
            }
        });
    }

    private int getRadioButtonId(int level) {
        int id = R.id.rb_understand_not;
        switch (level) {
            case 1:
                id = R.id.rb_understand_not;
                break;
            case 2:
                id = R.id.rb_understand_half;
                break;
            case 3:
                id = R.id.rb_understand;
                break;
        }
        return id;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_dialog_ok) {
            if (onCallBack != null) {
                onCallBack.updateMasteryLevel("masteryLevel", masteryLevel);
                dismiss();
            }
        }
    }

    private OnMasteryLevelCallBack onCallBack;

    public void setOnCallBack(OnMasteryLevelCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    public interface OnMasteryLevelCallBack {
        void updateMasteryLevel(String field, int level);
    }
}
